<template>
  <ex-dialog width="800px" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
        <el-form-item label="姓名：" prop="name" class="className">
          <el-input v-model="ruleForm.name" placeholder="请填写" maxlength="30"  :style="{ width: inputWidth }"
            show-word-limit />
        </el-form-item>
        <el-form-item label="手机号：" prop="phone">
          <el-input v-model="ruleForm.phone" placeholder="请填写" maxlength="11"  :style="{ width: inputWidth }" />
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-select v-model="ruleForm.status" placeholder="请选择" filterable :style="{ width: inputWidth }">
            <el-option v-for="item in statusList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      <el-button  @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="AgreeLoading" @click="submitForm('ruleForm')">确认</el-button>
    </div>
  </ex-dialog>
</template>
<script>
// api
import {
  addClientLiveUser, editClientLiveUser,clientLiveUserDetail
} from "@/api/customer/livePersonnelList";
import Loading from "@/components/Loading/index";
export default {
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  components: {
    Loading
  },
  data() {
    return {
      inputWidth: "400px",
      title: "新增主播",
      AgreeLoading: false,
      loading: false,
      ruleForm: {
        phone: '',
        name: '',
        status: '',
      },
      statusList: [],//状态列表
      rules: {
        name: [
          { required: true, message: "请填写姓名", trigger: ["change", "bulr"] }
        ],
        phone: [
          { required: true, message: "请填写手机号", trigger: ["change", "bulr"] }
        ],
        status: [
          { required: true, message: "请选择状态", trigger: ["change", "bulr"] }
        ],
      },
    };
  },
  computed: {},
  created() {
    this.getDict()
    if (this.pathType == 'edit') {
      this.title = '编辑主播'
      this.ruleForm.id = this.id
      this.getDataDetails()
    } else {
      this.title = '新增主播'
    }
  },
  mounted() {

  },
  computed: {},
  methods: {
    //获取状态字典
    getDict() {
      var dictType = "live_user_status";
      this.$getSelectList(dictType)
        .then((res) => {
          this.statusList = res.result[0].dictList;
        })
    },
    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    // 查看详情
    getDataDetails() {
      clientLiveUserDetail(this.id).then(response=>{
        this.ruleForm = response.result
      })
    },
    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.pathType == 'edit'){
            this.editData()
          }else{
            this.addData()
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 新增主播
    addData(){
      this.AgreeLoading = true
      var data = {
        ...this.ruleForm
      }
      addClientLiveUser(data).then(res => {
        this.$message.success('添加成功')
        setTimeout(() => {
          this.handleClose()
          this.$emit('success')
          this.AgreeLoading = false
        }, 1000);
      }).finally(() => {
        this.AgreeLoading = false
      })
    },
    // 编辑主播
    editData(){
      this.AgreeLoading = true
      var data = {
        ...this.ruleForm
      }
      editClientLiveUser(data).then(res => {
        this.$message.success('修改成功')
        setTimeout(() => {
          this.handleClose()
          this.$emit('success')
          this.AgreeLoading = false
        }, 1000);
      }).finally(() => {
        this.AgreeLoading = false
      })
    },
  }
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep {
    .className{
      .el-input__suffix {
        right: -45px;
      }
    }
    .ex--dialog--main {
      padding: 10px 20px !important;
    }

    .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }
  }
}

.main {
  padding: 20px 20px;
}

.button_box {
  display: flex;
  justify-content: center;
}
</style>
